﻿using System.Transactions;
using Bootstrap;
using DogshowRepository;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Bootstrap.AutoMapper;
using System.Linq;

namespace Dogshow.Linq2SQLRepository.Test
{
    [TestClass]
    public class TituleRepositoryFixtures
    {
        private ITituleRepository m_Cut;

        [ClassInitialize]
        public static void ClassInit(TestContext ctx)
        {
            var model = new DogshowModel();
            if (model.DatabaseExists())
            {
                model.DeleteDatabase();
            }
            model.CreateDatabase();
            model.ExecuteCommand("INSERT INTO Titule VALUES ('C.A.C')");
            model.ExecuteCommand("INSERT INTO Titule VALUES ('B.O.B')");
        }

        [TestInitialize]
        public void TestInit()
        {
            Bootstrapper.With.AutoMapper().Start();
        }

        [TestMethod]
        public void Insert()
        {
            using (new TransactionScope())
            {
                // Arrange
                m_Cut = new TituleRepository();
                var titula = new Model.Titula { Naziv = "C.A.C.I.B" };

                // Act
                m_Cut.Sacuvaj(titula);

                // Assert
                Assert.IsTrue(titula.Id > 0);
            }
        }

        [TestMethod]
        public void Update()
        {
            using (new TransactionScope())
            {
                // Arrange
                m_Cut = new TituleRepository();
                var titula = m_Cut.VratiPoId(1);
                titula.Naziv = "B.I.S";

                // Act
                m_Cut = new TituleRepository();
                m_Cut.Sacuvaj(titula);

                // Assert
                m_Cut = new TituleRepository();
                titula = m_Cut.VratiPoId(1);
                Assert.AreEqual("B.I.S", titula.Naziv);
            }
        }

        [TestMethod]
        public void Vrati_Sve_Titule()
        {
            // Arrange
            m_Cut = new TituleRepository();

            // Act
            var titule = m_Cut.VratiSve();

            // Assert
            Assert.IsTrue(titule.Count() > 0);
        }

        [TestMethod]
        public void Vrati_Po_Id()
        {
            m_Cut = new TituleRepository();
            var titula = m_Cut.VratiPoId(1);

            Assert.IsNotNull(titula);
            Assert.AreEqual("C.A.C", titula.Naziv);
        }
    }
}